package cart;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cart.Authentication;
import java.sql.*;

public class LoginServlet extends HttpServlet
{

    @Override
    public void doPost(HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException
    {
        String userid = request.getParameter("user_id") ;
        userid = userid.toLowerCase();
        String userpwd = request.getParameter("user_pwd");
        Authentication user = new Authentication(userid, userpwd);
        String hash = user.hashPwd();
        user.setpwd(hash);
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch(Exception E){
        }
        
        try {
               Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/eshop","root","");
               Statement Stmt = Conn.createStatement();
               ResultSet rs  = Stmt.executeQuery("SELECT * FROM member WHERE login_name =\""+ userid + "\";");
               rs.next();
               user.setdbpwd(rs.getString("password"));
         }catch (SQLException E){
         }
   
        
        if(user.chekLogin())
            request.setAttribute("result", 1);
        else
            request.setAttribute("result", 0);

        String url="/boolResult.jsp";
        ServletContext sc = getServletContext();
        RequestDispatcher rd = sc.getRequestDispatcher(url);
        rd.forward(request, response);
    }
} 
